function DB() {
    let db = null;

    async function openDB() {
        return new Promise((resolve, reject) => {
            if (db) {
                resolve(db);
                return;
            }
            var request = indexedDB.open('token');
            request.onerror = function (event) {
                reject(false);
            };
            request.onsuccess = function (event) {
                db = event.target.result;
                resolve(db);
            };
            request.onupgradeneeded = function (event) {
                var db = event.target.result;
                var objectStore = db.createObjectStore("token", {keyPath: "id"});
                objectStore.transaction.oncomplete = function (event) {
                    resolve(db);
                };
            };
        });
    }

    async function getToken() {
        if (!db) db = await openDB();
        var request = db.transaction(["token"]).objectStore("token").get(0);
        return new Promise((resolve, reject) => {
            request.onerror = function (event) {
                resolve(null);
            };
            request.onsuccess = function (event) {
                let token = null;
                if (request.result && request.result.token) token = request.result.token;
                resolve(token);
            };
        });
    }

    async function setToken(val) {
        if (!db) db = await openDB();
        var request = db.transaction(["token"], "readwrite").objectStore("token").put({id: 0, token: val});
        return new Promise((resolve, reject) => {
            request.onerror = function (event) {
                resolve(null);
            };
            request.onsuccess = function (event) {
                // 对 request.result 做些操作！
                resolve('success');
            };
        });
    }

    return {
        getToken,
        setToken
    };

}